package com.woniuxy.oldmanapi.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.wf.captcha.base.Captcha;
import com.woniuxy.oldmanapi.commons.LoginCodeEnum;
import com.woniuxy.oldmanapi.commons.LoginProperties;
import com.woniuxy.oldmanapi.entity.Manager;
import com.woniuxy.oldmanapi.service.ManagerService;
import com.woniuxy.oldmanapi.utils.JwtUtils;
import com.woniuxy.oldmanapi.vo.AccountVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class ManagerController {
    @Autowired
    ManagerService managerService;
    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;


    @PostMapping("/manager/login")
    public ResponseResult<Manager> login(@RequestBody AccountVo accountVo) {
        ResponseResult<Manager> result = new ResponseResult<>();
        try {
            //获取验证码
            Object o = redisTemplate.opsForValue().get(accountVo.getCodeUuid());
            if (o == null) {
                log.info("验证码过期---->" + accountVo.getCode());
            } else {
                if (accountVo.getCode().equals(o.toString())) {
                    log.info("验证码正确---->" + accountVo.getCode());
                    Manager manager = managerService.login(accountVo.getUsername());
                    if (manager == null) {
                        result.setCode(201);
                        result.setMsg("账户不存在!!!");
                    } else {
                        if (manager.getPassword().equals(accountVo.getPassword())) {
                            result.setMsg("登录成功!!!");
                            result.setCode(200);
                            result.setData(manager);
                            //生成 token
                            accountVo.setId(manager.getId());
                            String token = jwtUtils.createToken(accountVo);
                            result.setToken(token);
                        } else {
                            result.setCode(202);
                            result.setMsg("密码错误!!!");
                        }
                    }
                } else {
                    log.info("验证码错误---->" + accountVo.getCode());
                    result.setCode(204);
                    result.setMsg("验证码错误，请重新输入!!!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    @GetMapping("/manager/code")
    public Object getCode() {
        LoginProperties loginProperties = new LoginProperties();
        Captcha captcha = loginProperties.getCaptcha();
        String uuid = "woniuxy" + IdUtil.simpleUUID();
        //当验证码类型为 arithmetic时且长度 >= 2 时，captcha.text()的结果有几率为浮点型
        String captchaValue = captcha.text();
        if (captcha.getCharType() - 1 == LoginCodeEnum.ARITHMETIC.ordinal() && captchaValue.contains(".")) {
            captchaValue = captchaValue.split("\\.")[0];
        }
        // 保存
        //redisUtils.set(uuid,captchaValue,loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(uuid, captchaValue, loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES);
        // 验证码信息
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {{
            put("img", captcha.toBase64());
            put("uuid", uuid);
        }};
        return imgResult;
    }
}
